home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group99a.txt / 000131_icon-group-sender _Tue Jun 8 17:06:25 1999.msg < prev    next >
Internet Message Format  |  2000-09-20  |  8KB

  1. Return-Path: <icon-group-sender>
  2. Received: (from root@localhost)
  3.     by baskerville.CS.Arizona.EDU (8.9.1a/8.9.1) id RAA00220
  4.     for icon-group-addresses; Tue, 8 Jun 1999 17:05:52 -0700 (MST)
  5. Message-Id: <199906090005.RAA00220@baskerville.CS.Arizona.EDU>
  6. Delivered-To: icon-group@silliac.cs.arizona.edu
  7. Date: Tue, 8 Jun 1999 15:41:22 -0500 (CDT)
  8. From: "John C. Paolillo" <johnp@ling.uta.edu>
  9. To: CHETHCOA@oss.oceaneering.com, icon-group@optima.CS.Arizona.EDU
  10. Subject: Re: Fwd: Re: File Translators in Icon
  11. Cc: john@ling.uta.edu
  12. Errors-To: icon-group-errors@optima.CS.Arizona.EDU
  13. Status: RO
  14.  
  15. I don't know if this will help, but I once had reason to
  16. write the following Icon program to assist in typesetting
  17. Feature-Attribute matrices in MS Word.  In MS Word for mac
  18. v 4.0 and 5.0, there is a family of mathematical typesetting
  19. formulas (listed unde "Formula" in the user manuals) which
  20. employ a special typesetting character (that turns out to be 
  21. ASCII 006) as an escape character.  The formulas are acceptably
  22. typeset (or they were for my purposes) but very cumbersome to
  23. type.  I wrote this program to translate from a more transparent
  24. syntax into the form actually used by MS Word.  As for newer
  25. versions of MS Word, I don't know if you can type the formula
  26. typesetting sequences directly (I couldn't figure out how to
  27. do it), but my files with the complex formulas in them that
  28. were created in MS Word v 4.0 still display correctly when
  29. converted.  
  30.  
  31. I haven't tried this in a long time, so I am not 100% certain
  32. if this is the best version.  There are probably improvements
  33. that can be made to it at any rate.  Some of the typesetting
  34. that is possible (e.g. integrals) are not implemented, but that
  35. could easily be added by referring to the appropriate pages in 
  36. the Word manuals.  The program was written to be used as a 
  37. clipboard filter with the macintosh interpreter IClip.
  38.  
  39. John C. Paolillo
  40. Program in Linguistics
  41. University of Texas at Arlington
  42.  
  43. ##########################################################################
  44. #
  45. #    This program parses a description of a attribute-value matrix using
  46. #    ordinary ascii characters (using upper and lowercase letters for 
  47. #    feature values and labels, [] to delimit matrices, {} to delimit sets
  48. #    and <> to delimit lists), and outputs a string of the appropriate
  49. #    description, using MS Word's formula typesetting expressions.  The
  50. #    resulting string, when pasted into a Word document, will appear in
  51. #    the correctly typeset form.  For example, the following description
  52. #
  53. #    [    [    this    |    is    ]
  54. #        [    an        |    example 
  55. #            of        |    typesetting]    ]
  56. #
  57. #    Will be typeset as below (ascii impression).
  58. #
  59. #        +-                             -+
  60. #        |    [ this | is ]              |
  61. #        |    +-                       -+ |
  62. #        |    |    an | example        | |
  63. #        |    |    of | typesetting    | |
  64. #        |    +-                       -+ |
  65. #        +-                             -+
  66. #
  67. #    The program will parse as many such expressions as are in the input,
  68. #    until it encounters a non well-formed description.  All remaining
  69. #    input (well formed or not) is discarded.  
  70. #
  71. ########################################################################
  72.  
  73. global white_sp            #    white space character set
  74.  
  75. procedure main()
  76.     white_sp := ' \t'        #    white space is tab and space
  77.     line := ""
  78.     while line ||:= read()    #    concatenate input into one long string
  79.                             #    (removes return/new line characters)
  80.     line ?                    #    and scan the line for parsing
  81.         {
  82.             while a_parse := parse() do
  83.                 {            #    write each parse as a separate line
  84.                     write(lst_2_str(a_parse))
  85.                 }
  86.         }
  87. end
  88.  
  89. #    This procedure converts an input list (a parse) into a formatted
  90. #    string.  Many variations are possible.  This one replaces the 
  91. #    string, list and set delimiter characters with the appropriate
  92. #    typesetting commands.  The "" character below is ascii 006, Word's
  93. #    formula typesetting command character.  Using backslash to represent
  94. #    this character, the strings replacing the delimiters are:
  95. #
  96. #        [    \b\bc\[(\a\al(        # begin a bracket delimited by "[]"
  97. #                                # with a left-alligned array as argument
  98. #
  99. #        <    \b\bc\<(\a\ac(        # begin a bracket delimited by "<>"
  100. #                                # with a center-alligned array as argument
  101. #
  102. #        {    \b\bc\[(\a\ac(        # begin a bracket delimited by "{}"
  103. #                                # with a center-alligned array as argument
  104. #
  105. #        ]    ))                    # close two argument lists
  106. #        >    ))
  107. #        }    ))
  108. #    
  109. procedure lst_2_str(alist)
  110.     if x := string(alist) then    #    don't convert it if its already a string
  111.         return case x of
  112.             {                    #    do the above replacements
  113.                 "["        :    "bbc[(aal("
  114.                 "<"        :    "bbc<(aac("
  115.                 "{"        :    "bbc{(aac("
  116.                 "]"        :    "))"
  117.                 ">"        :    "))"
  118.                 "}"        :    "))"
  119.                 default    :    x    #    default to the incoming value
  120.             }
  121.     else                        #    if it's a list then convert it
  122.         {
  123.             mid_str := ""        #    mnemonically "middle string"
  124.             every x := !alist do
  125.                 {                #    iterate through the arguments
  126.                     mid_str ||:= lst_2_str(x)    #    concatenate
  127.                     mid_str ||:= " "            #    pad with space
  128.                 }
  129.             return mid_str[1:-1]    #    don't include the last space
  130.         }
  131. end
  132.  
  133. #    the parser looks for each of the following possibilities
  134. #    label    -- a string of letters
  135. #    expr    -- a label followed by a value
  136. #    box        -- labels with values inside []
  137. #    l_list    -- boxes grouped inside <>
  138. #    set        -- boxes grouped inside {}
  139. procedure parse()
  140.     suspend label() | expr() | box() | set() | l_list()
  141. end
  142.  
  143. #    a label is a bunch of letters together with no whitespace or punctuation
  144. #    no digits are allowed
  145. procedure label()
  146.     suspend tab(many(&letters))
  147. end
  148.  
  149. #    an expression is a label with a value.  The value may be
  150. #    a label (separated by "|"), a box, a set a list or another expression
  151. procedure expr()
  152.     suspend [ one_of(label) ] |||    (    [ ="|", one_of(label) ] |
  153.                                         [ one_of( box | set | l_list | expr ) ] )
  154. end
  155.  
  156. #    a box is some arbitrary number of expressions enclosed in []
  157. #    the parsed expressions will have "," items between them in the parse
  158. #    so that Word will know to typeset them vertically in the array.  
  159. #    DO NOT use the commas in your description, it will not parse if you do.
  160. #    box()es must have at least one exprtession in them
  161. procedure box()
  162.     suspend [ ="[" ] ||| com_arbno(expr) ||| [ ="]" ]
  163. end
  164.  
  165. #    a set is an arbitrary number of boxes enclosed in {} -- the same
  166. #    caveats apply as for box()
  167. procedure set()
  168.     suspend [ ="{" ] ||| com_arbno(box) ||| [ ="}" ]
  169. end
  170.  
  171. #    a l_list is an arbitrary number of boxes enclosed by <>, with no commas
  172. #    between them so Word will know to typeset them horizontally.  There
  173. #    may be zero items in a l_list.  
  174. procedure l_list()
  175.     suspend [ ="<" ] ||| arbno(box) ||| [ =">" ]
  176. end
  177.  
  178. #    com_abrno() produces a list containing an arbitrary number of 
  179. #    some matching expression p, separated by one fewer "," items
  180. #    on the list than there are p's.  This is for things that need to
  181. #    be typeset vertically by Word.  The base case is a single p, with
  182. #    no commas in the putput list.
  183. procedure com_arbno(p)
  184.     suspend [one_of(p)] ||| ( [] | ( [ "," ] ||| arbno(p) ) )
  185. end
  186.  
  187. #    arbno() produces a list containing an arbitrary number of some
  188. #    matching procedure p.  This is for things that need to be typeset 
  189. #    horizontally.  The base case is an empty list
  190. procedure arbno(p)
  191.     suspend [] | [one_of(p)] ||| arbno(p)
  192. end
  193.  
  194. #    one_of() produces a single match of the expression p, discarding
  195. #    any surrounding whitespace it encounters.  This is a useful technique
  196. #    to use in a parser, with a little modification, it can also be
  197. #    used to get optional constituents.
  198. procedure one_of(p)
  199.     tab(many(white_sp)) 
  200.     x <- p()
  201.     tab(many(white_sp))
  202.     suspend \x
  203. end
  204.  
  205.